package ch.msoftch.gui.utils.popup.actions;

import org.eclipse.jdt.core.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.*;
import ch.msoftch.gui.utils.*;

/**
 * Action used to create property constants for a java bean.<br>
 * This action is registred for {@link ICompilationUnit}s.
 * 
 * @author hautle
 */
public class CreatePropertyFieldsAction implements IObjectActionDelegate {
	/** The shell. */
	private Shell shell;

	/** The current selection. */
	private IStructuredSelection selection;

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		try {
			PropertyClassEnhancer.enhanceClass((ICompilationUnit) selection.getFirstElement());
		} catch (JavaModelException e) {
			EclipseHelper.showError(shell, "Error while enhancing the class", e);
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = (IStructuredSelection) selection;
	}
}
